<template>
	<view class="content">
		<view class="box">
			<view class="item">
				<text>账号</text>
				<uni-easyinput type="number" v-model="phoneNumber" placeholder="请输入账号"></uni-easyinput>
			</view>
			<view class="item">
				<text>密码</text>
				<uni-easyinput type="password" v-model="password" placeholder="请输入密码"></uni-easyinput>
			</view>
			<view class="tips">
				<radio value="" :checked="checked" @click="changeRadio" style="transform: scale(0.7)" />
				<text>记住密码</text>
			</view>
			<view class="btn" @click="Login">登录</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			checked: false,
			token: '',
			phoneNumber: '',
			password: '',
			showPassword: false
		};
	},
	onLoad() {
		if (uni.getStorageSync('phoneNumber')) {
			this.phoneNumber = uni.getStorageSync('phoneNumber');
			this.password = uni.getStorageSync('password');
			this.checked = true;
		}
		this.getToken();
	},
	methods: {
		changeRadio() {
			this.checked = !this.checked;
		},

		changePassword: function () {
			this.showPassword = !this.showPassword;
		},

		Login() {
			if (!this.phoneNumber) {
				uni.showToast({
					icon: 'none',
					title: '请输入账号'
				});
				return;
			}
			if (!this.password) {
				uni.showToast({
					icon: 'none',
					title: '请输入密码'
				});
				return;
			}
			this.$axios
				.get('/application/login', {
					params: {
						token: this.token,
						phoneNumber: this.phoneNumber,
						password: this.password
					}
				})
				.then((res) => {
					console.log(res, '登陆的数据');
					if (this.checked) {
						uni.setStorageSync('phoneNumber', this.phoneNumber);
						uni.setStorageSync('password', this.password);
					} else {
						uni.removeStorageSync('phoneNumber');
						uni.removeStorageSync('password');
					}
					uni.setStorageSync('userinfo', res.data[0]);
					uni.setStorageSync('token', this.token);
					// uni.startLocationUpdate()
					uni.reLaunch({
						url: '/pages/index/index'
					});
				},err=>{
					uni.showToast({
						icon: "none",
						title: "账号、密码错误"
					})
				});
		},

		getToken() {
			this.$axios
				.get('/secret/token', {
					params: {
						name: 'locationApp',
						key: '75f2d420ef91917567b7934d56557b2d2ab81f29'
					}
				})
				.then((res) => {
					console.log(res, '查询的token');
					this.token = res.data.token;
				});
		}
	}
};
</script>

<style lang="scss" scoped>
.content {
	width: 750rpx;
	height: 100vh;
	display: flex;
	align-items: center;
	justify-content: center;
	.box {
		width: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.item {
			width: 600rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			margin-bottom: 20rpx;
			text {
				margin-right: 20rpx;
			}
			input {
				flex: 1;
				height: 80rpx;
				border: 1rpx solid #333;
				border-radius: 10rpx;
				padding: 0 10rpx;
				box-sizing: border-box;
			}
		}

		.tips {
			width: 600rpx;
			display: flex;
			margin-bottom: 80rpx;
			radio {
				margin-left: 2rem;
			}
			text {
			}
		}
		.btn {
			width: 600rpx;
			height: 90rpx;
			background-color: #1e98d7;
			text-align: center;
			line-height: 90rpx;
			color: #fff;
			border-radius: 10rpx;
		}
	}
}
</style>
